package com.wbao.dianniu.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wbao.dianniu.R;
import com.wbao.dianniu.data.QuestAnswerCountsData;
import com.wbao.dianniu.data.QuestAnswerListData;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.AnswerPraiseManager;
import com.wbao.dianniu.utils.PictureDownloadUtils;

/**
 * 匿名问答详情
 * 
 * @author yejianjun
 *
 */
public class AmDetailAdapter extends BaseAdapter {

	private Context mContext;
	private List<QuestAnswerListData> mList;
	
	public AmDetailAdapter(Context context) {
		mContext = context;
	}
	
	public void addData(List<QuestAnswerListData>  list,QuestAnswerCountsData qacd){
		if(null == mList){
			mList = new ArrayList<QuestAnswerListData>();
		}
		for (int i = 0; i < list.size(); i++) {
			mList.add(list.get(i));
		}
	}
	public List<QuestAnswerListData> getList() {
		return mList;
	}
	public void addOneData(QuestAnswerListData data){
		if(null != mList){
			mList.add(0, data);
		}
	}
	
	public void deleteOneData(int position){
		mList.remove(position);
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		if (null == mList)
			return 0;
		else
			return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder = null;
		if(null == convertView){
			LayoutInflater mInflater = LayoutInflater.from(mContext);
			convertView = mInflater.inflate(R.layout.quest_answer_list_item, null);   
			holder = new Holder();
			holder.head = (ImageView) convertView.findViewById(R.id.quest_answer_head);
			holder.name = (TextView) convertView.findViewById(R.id.quest_answer_name);
			holder.label = (TextView) convertView.findViewById(R.id.quest_answer_label);
			holder.isGood = (ImageView) convertView.findViewById(R.id.quest_answer_good_button);
			holder.goodCounts = (TextView) convertView.findViewById(R.id.quest_answer_good_counts);
			holder.content = (TextView) convertView.findViewById(R.id.quest_answer_content);
			convertView.setTag(holder);
		}else{
			holder = (Holder) convertView.getTag();
		}
		if(null == mList || mList.size() < position){
			return convertView;
		}
		QuestAnswerListData data = mList.get(position);
		if(null != data){
			PictureDownloadUtils.displayHeadImage(mContext,holder.head, data.headPic);
			holder.name.setText(data.aliasName == null ? "" :data.aliasName);
			holder.label.setVisibility(View.GONE);
			if(1 == data.isGood){
				holder.isGood.setImageResource(R.drawable.click_praise_icon_sel);
			}else{
				holder.isGood.setImageResource(R.drawable.click_praise_icon_nor);
			}
			holder.goodCounts.setText(data.goodCount+"");
			holder.content.setMovementMethod(LinkMovementMethod.getInstance());
			holder.content.setAutoLinkMask(Linkify.WEB_URLS); 
			holder.content.setText(data.content == null ? "" : data.content);
		}
		holder.isGood.setOnClickListener(new praiseListener(position,data));
		return convertView;
	}
	
	 /**
	 *		问题回答点赞
	 */
	class praiseListener implements OnClickListener{
		 private int position;
		 private QuestAnswerListData data;
		 public praiseListener(int position,QuestAnswerListData data) {
			 this.position = position;
			 this.data = data;
		}
		@Override
		public void onClick(View v) {
			if(v.getId() == R.id.quest_answer_good_button){
				if(1 == data.isGood){
					data.goodCount --;
					data.isGood = 0;
				}else{
					data.goodCount ++;
					data.isGood = 1;
				}
				notifyUI(position, data);
				//通知后台更新数据
				AnswerPraiseManager manager = new AnswerPraiseManager(mContext);
				manager.answerPraise(GlobalContext.accountId, data.questId, data.id);
			}
			
		}
		
	}
	
	 private void notifyUI(int position,QuestAnswerListData data){
		 if(null != data && null != mList){
			 mList.set(position, data);
		 }
		 notifyDataSetChanged();
	 }
	 
	private class Holder{
		public ImageView head;
		public TextView name;
		public TextView label;
		public ImageView isGood;
		public TextView goodCounts;
		public TextView content;
	}
	
}
